// Read the Phantom webpage '#intro' element text using jQuery and "includeJs"

"use strict";
var page = require('webpage').create();
phantom.outputEncoding='gbk'
page.onConsoleMessage = function(msg) {
    console.log(msg);
};
page.viewportSize  = {
  width: 320,
  height: 480
};
phantom.setProxy('123.134.87.136', '61234', 'manual', '', '');
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36';
page.open("http://www.ip138.com/", function(status) {
    console.log(status)
    if (status === "success") {
        // document.body.style.width=320
        // document.body.style.height=480
        // page.clipRect = { top: 0, left: 0, width: 320, height: 480 };
        page.render('capture1.png');  

       setTimeout(function(){
     console.log("currentFrameName(): "+page.currentFrameName());
    console.log("childFramesCount(): "+page.childFramesCount());
    console.log("childFramesName(): "+page.childFramesName());

           page.switchToChildFrame("<!--framePath //<!--frame0-->-->")             
          
          // page.includeJs("https://code.jquery.com/jquery-1.6.1.min.js", function() {
            if (page.injectJs('jquery.min.js')) {
                 var result = page.evaluate(function() {
                 console.log(document.title)    
                 console.log($('center').text())     
                //  console.log(222)
            //     console.log($('#kw'))
            //     $('#kw').val('张三他爸')
            // //     console.log(1111)
            //     // $('#su').val('百度二下')
            //     $('#search-btn').click()
            //     // $('#su').click()
            //     // $('#u1 a.mnav').click()
            //     console.log(document.body.scrollHeight/10)
            //     var t=setTimeout(function(){   
            //      console.log(document.body.scrollHeight/10)       
                //  for (var i = 0; i < 10; i++) {
                //      window.scrollBy(0,document.body.scrollHeight/10)   
                // }
            // },2000)
                return 'capture2.png'
                // console.log("$(\".explanation\").text() -> " + $(".explanation").text());
                }); 

            }else{
              console.log('注入jquery失败')
            }

               // $('#kw').val('张三他爸')
               // $('#su').click()
               // $('#u1 a.mnav').click()
                setTimeout(function(){
                page.render(result) 
                phantom.exit(0);    
                },8000)
        },5000);
       
    } else {
      phantom.exit(1);
    }
});
